package resourceServer

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/Logic"
	"server/Project_User"
	"server/middleWare"
	"server/response"
	"strconv"
)

func ResourceServer(port string) {

	r := gin.Default()
	r.Use(middleWare.CORS())
	//用来获取资源信息
	r.GET("getResourceInfo", func(ctx *gin.Context) {
		id, _ := strconv.Atoi(ctx.Query("userid"))
		info, err := Logic.GetResourceInfo(ctx, id)
		if err != nil {
			fmt.Println(err)
		}

		response.Success(ctx, gin.H{"resourceInfo": info}, "成功获得资源信息")

	})

	r.GET("updateResourceInfo", func(ctx *gin.Context) {
		id, _ := strconv.Atoi(ctx.Query("userid"))
		diamond := ctx.Query("diamond")
		stone := ctx.Query("stone")
		iron := ctx.Query("iron")
		gold := ctx.Query("gold")
		goldCoin := ctx.Query("goldCoin")
		silverCoin := ctx.Query("silverCoin")
		var resource Project_User.UpdateResource
		resource.Diamond = diamond
		resource.Stone = stone
		resource.Iron = iron
		resource.Gold = gold
		resource.GoldCoin = goldCoin
		resource.SilverCoin = silverCoin

		_ = Logic.UpdateResourceCount(ctx, id, resource)
		response.Success(ctx, nil, "资源信息更新成功")

	})
	r.Run(":" + port)
}
